package com.letv.core.network.volley;

import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.text.TextUtils;

import com.letv.core.network.volley.VolleyRequest.HttpRequestMethod;
import com.letv.core.network.volley.listener.HttpStack;
import com.letv.core.utils.LetvUtils;
import com.letv.core.utils.LogInfo;

/**
 * httpclient方式执行请求
 * 
 * @author zhuqiao
 * 
 */
public class HttpClientStack implements HttpStack {
    private final HttpClient mClient;

    public HttpClientStack(HttpClient client) {
        mClient = client;
    }

    @Override
    public HttpResponse performRequest(VolleyRequest<?> request) {
        if (TextUtils.isEmpty(request.getUrl())) {
            return null;
        }

        try {
            HttpUriRequest httpRequest = createHttpRequest(request);

            // 添加头信息
            Map<String, String> headers = request.mHeadMap;
            if (headers != null && headers.size() > 0) {
                for (String headerName : headers.keySet()) {
                    httpRequest.setHeader(headerName, headers.get(headerName));
                }
            }

            String ua = LetvUtils.getUa();
            if (!TextUtils.isEmpty(ua)) {
                httpRequest.setHeader("User-agent", ua);
            }

            // 添加超时
            HttpParams httpParams = httpRequest.getParams();
            HttpConnectionParams.setConnectionTimeout(httpParams, request.mTimeOut);
            HttpConnectionParams.setSoTimeout(httpParams, request.mTimeOut);

            return mClient.execute(httpRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    static HttpUriRequest createHttpRequest(VolleyRequest<?> request) throws Exception {
        HttpRequestMethod method = request.mHttpRequestMethod;
        if (method == HttpRequestMethod.GET) {
            return new HttpGet(request.getUrl());
        } else if (method == HttpRequestMethod.POST) {
            return getHttpPost(request, request.getPostBody());
        } else {
            // 默认auto
            byte[] body = request.getPostBody();
            if (body != null) {
                return getHttpPost(request, body);
            } else {
                return new HttpGet(request.getUrl());
            }
        }
    }

    private static HttpPost getHttpPost(VolleyRequest<?> request, byte[] body) throws Exception {
        HttpPost post = new HttpPost(request.getUrl());
        post.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        addBodyIfExists(post, body);
        return post;
    }

    private static void addBodyIfExists(HttpEntityEnclosingRequestBase httpRequest, byte[] body) throws Exception {
        if (body == null) {
            return;
        }
        HttpEntity entity = new ByteArrayEntity(body);
        httpRequest.setEntity(entity);
    }
}
